"""
public相关的api接口
"""
from flask import jsonify,request

from main.user.models import User


from . import bp

@bp.route('/api/register_user',methods=['POST'])
def register_user():
    """
    注册用户
    """
    #推荐人
    rename = request.form['rename']
    #会员名称
    truename = request.form['truename']
    #手机号
    mobile = request.form['mobile']
    #注册购币
    signmoney = request.form['signmoney']


    rename = User.query.filter_by(username=rename).first()
    if not rename:
        return jsonify({'code':0,'url':'','msg':'没有该推荐人。'})

    user = User.query.filter_by(username=mobile).first()
    if user:
        return jsonify({'code':0,'url':'','msg':'该手机号已被注册。'})

    if not rename.repath:
        rename.repath = ''
    try:
        User.create(
            username = mobile,
            phone = mobile,
            first_name = truename,
            password = '123456',
            passcheck = '123456',
            reid = rename.id,
            repath = rename.repath+','+str(rename.id),
            regmoney = float(signmoney)
        )
    except Exception as e:
        return jsonify({'code':0,'url':'','msg':f'注册失败,请稍后尝试。'})
    

    return jsonify({'code':1,'url':'','msg':'注册完成'})



